import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'biologeek.settings'
from django.conf import settings
from slughifi import slughifi

from biologeek.journal.models import Post
for post in Post.objects.all(): post.delete()

from biologeek.bistrot.models import Thought
for thought in Thought.objects.all(): thought.delete()

from django.contrib.comments.models import FreeComment
for comment in FreeComment.objects.all(): comment.delete()

from django.contrib.contenttypes.models import ContentType

def decode_html(content):
    content = content.replace('\\"', '"')
    content = content.replace('\\n', '\n')
    content = content.replace('\\r', '\r')
    return content

backup = open('blog-backup.txt')
blog = {}
for table in backup.read().split('\n',1)[1].strip().split('\n\n'):
    header, content = table.split('\n',1)
    table_name, column_names = header[1:-1].split(' ')
    column_names = tuple(column_names.split(','))
    print table_name, column_names
    blog[table_name] = {}
    for item in content.split('\n'):
        column_contents = tuple((decode_html(column) for column in item[1:-1].split('","')))
        item_dict = dict(zip(column_names, column_contents))
        if table_name in ('post_meta',):
            id = item_dict[column_names[1]] # post_id
        else:
            id = item_dict[column_names[0]] # `table`_id
        blog[table_name][id] = item_dict

for post_dict in blog['post'].values():#[:10]:
    django_post = Post()
    django_post.id = post_dict['post_id']
    django_post.title = post_dict['post_titre']
    django_post.slug = post_dict['post_titre_url']
    django_post.tags = blog['categorie'][post_dict['cat_id']]['cat_libelle_url'].lower()

    django_post.summary_html = post_dict['post_chapo']
    django_post.summary = post_dict['post_chapo_wiki']
    django_post.content_html = post_dict['post_content']
    django_post.content = post_dict['post_content_wiki']

    django_post.creation_date = post_dict['post_creadt']
    django_post.modification_date = post_dict['post_upddt']
    django_post.publication_date = post_dict['post_dt']

    django_post.is_online = post_dict['post_pub']
    django_post.is_bestof = post_dict['post_selected']
    django_post.markup = 'wiki2xhtml'
    try:
        django_post.image = 'logos/%s.png' % blog['post_meta'][post_dict['post_id']]['meta_value']
    except KeyError:
        title = post_dict['post_titre'].lower()
        if 'django' in title:
            django_post.image = 'logos/django.png'
        elif 'python' in title:
            django_post.image = 'logos/python_nouveau.png'
        elif 'biologeek' in title:
            django_post.image = 'logos/biologeek.png'
        elif 'ubuntu' in title:
            django_post.image = 'logos/ubuntu.png'
        else:
            print "No image for post %s" % post_dict['post_titre']

    django_post.save()

print Post.objects.all()

for thought_dict in blog['blogmark'].values()[:10]:
    django_thought = Thought()
    django_thought.title = thought_dict['title']
    django_thought.slug = slughifi(thought_dict['title'])
    django_thought.user_id = 1

    django_thought.content = thought_dict['comment']
    django_thought.content_html = thought_dict['comment']

    django_thought.creation_date = thought_dict['date']
    django_thought.modification_date = thought_dict['date']
    django_thought.publication_date = thought_dict['date']

    django_thought.is_online = True

    django_thought.save()

print Thought.objects.all()

for thought_dict in blog['blogmark2'].values()[:10]:
    django_thought = Thought()
    django_thought.title = thought_dict['title']
    django_thought.slug = slughifi(thought_dict['title'])
    django_thought.user_id = 1

    comment = '<p><a href="%s" title="Lire le billet (sortie)">%s</a></p>' \
                % (thought_dict['url'], thought_dict['comment'])

    django_thought.content = comment
    django_thought.content_html = comment

    django_thought.creation_date = thought_dict['date']
    django_thought.modification_date = thought_dict['date']
    django_thought.publication_date = thought_dict['date']

    django_thought.is_online = True

    django_thought.save()

print Thought.objects.all()

post_content_type = ContentType.objects.get_for_model(Post)

for comment_dict in blog['comment'].values()[:500]:
    if not int(comment_dict['comment_trackback']):
        django_comment = FreeComment()
        django_comment.id = int(comment_dict['comment_id'])
        django_comment.content_type = post_content_type
        django_comment.object_id = int(comment_dict['post_id'])
        django_comment.comment = comment_dict['comment_content'].decode('utf-8').encode('utf-8')
        django_comment.person_name = comment_dict['comment_auteur']
        django_comment.submit_date = comment_dict['comment_dt']
        django_comment.is_public = int(comment_dict['comment_pub'])
        django_comment.ip_address = comment_dict['comment_ip']
        django_comment.approved = 1
        django_comment.site_id = 1
        django_comment.save()

print FreeComment.objects.all()
comment = FreeComment.objects.all()[0]
print comment.comment, comment.ip_address, comment.submit_date, comment.id, comment.content_type, comment.object_id, comment.person_name, comment.is_public, comment.approved, comment.site_id

